﻿Public Class SBRGT
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub SBRGT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim s As FILTERCLASS
        'Dim sql As String = "SELECT CAST(MTRGTID AS CHAR(4)) AS MTRGTID, MTRGTNAME FROM masmtypergt WHERE STATUS = '1' AND SDET = '1'"
        Dim sql As String = "SELECT CAST(a.mtrgtid AS CHAR(4)) AS MTRGTID, a.mtrgtname FROM masmtypergt a LEFT JOIN masstypergt b ON a.mtrgtid = b.mtrgtid WHERE a.status = '1' AND a.sdet = '1' AND b.sdet = '1' GROUP BY a.`mtrgtid`"
        s = New FILTERCLASS(MTRGTTxt, sql, "รหัสหมวดคู่สัญญา,ชื่อหมวดคู่สัญญา", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        querySBRGTGrid()

        DesignForm(Me)
        DesignGrid(MASSUBRGTGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Public Sub querySBRGTGrid()
        Dim objSelectDataMastypergt As New mastypergt
        Dim massubrgtData As DataTable
        massubrgtData = connect.GetTable(objSelectDataMastypergt.selectMassubrgt_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        MASSUBRGTGrid.PrimaryGrid.DataSource = massubrgtData
    End Sub

    '---------filter right------------

    Private Sub queryFilterGrid(mtCode As String, stCode As String)
        Dim sql As String
        sql = "SELECT c.`sbrgtid` AS 'ID สิทธิ์', a.`mtrgtname` AS 'ชื่อหมวดคู่สัญญา', b.`strgtname` AS 'ชื่อกลุ่มคู่สัญญา', c.`subrgtname` AS 'ชื่อสิทธิ์', c.`status` AS 'สถานะการเปิดใช้งาน', a.`mtrgtid` AS 'รหัสหมวด', b.`strgtid` AS 'รหัสกลุ่ม' FROM massubrgt c JOIN masstypergt b ON c.strgtid = b.strgtid JOIN masmtypergt a ON b.mtrgtid = a.mtrgtid "

        If mtCode IsNot Nothing And stCode IsNot Nothing Then
            sql += "WHERE a.`mtrgtid` = '" & mtCode & "' AND b.`strgtid` = '" & stCode & "';"
        ElseIf mtCode IsNot Nothing And stCode Is Nothing Then
            sql += "WHERE a.`mtrgtid` = '" & mtCode & "';"
        ElseIf mtCode Is Nothing And stCode IsNot Nothing Then
            sql += "WHERE b.`strgtid` = '" & stCode & "';"
        End If

        Dim dt As DataTable
        dt = connect.GetTable(sql)
        MASSUBRGTGrid.PrimaryGrid.DataSource = dt
    End Sub

    Private Sub filterGridChk_CheckedChanged(sender As Object, e As EventArgs) Handles mtGridChk.CheckedChanged
        If mtGridChk.Checked = True Or stGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag)
        Else
            querySBRGTGrid()
        End If
    End Sub

    Private Sub stGridChk_CheckedChanged(sender As Object, e As EventArgs) Handles stGridChk.CheckedChanged
        If mtGridChk.Checked = True Or stGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag)
        Else
            querySBRGTGrid()
        End If
    End Sub

    '---------------------------------

    Private Sub MTRGTTxt_Click(sender As Object, e As EventArgs) Handles MTRGTTxt.Click
        MTRGTTxt.Text = ""
        MTRGTTxt.Tag = ""
    End Sub

    Private Sub MTRGTTxt_TextChanged(sender As Object, e As EventArgs) Handles MTRGTTxt.TextChanged
        Dim s2 As FILTERCLASS
        Dim sql2 As String = "SELECT CAST(strgtid AS CHAR(4)) AS STRGTID, strgtname FROM masstypergt WHERE status = '1' AND sdet = '1' AND mtrgtid = '" & MTRGTTxt.Tag & "'"
        s2 = New FILTERCLASS(STRGTTxt, sql2, "รหัสกลุ่มคู่สัญญา,ชื่อกลุ่มคู่สัญญา", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s2.SetShowBorder = True
    End Sub

    Private Sub STRGTTxt_Click(sender As Object, e As EventArgs) Handles STRGTTxt.Click
        STRGTTxt.Text = ""
        STRGTTxt.Tag = ""
    End Sub

    Private Sub MASSUBRGTGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASSUBRGTGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        SBRGTIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ID สิทธิ์").Value()
        MTRGTTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหมวด").Value()
        MTRGTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหมวดคู่สัญญา").Value()
        STRGTTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม").Value()
        STRGTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มคู่สัญญา").Value()
        SBRGTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสิทธิ์").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            massubrgtStatusChk.Checked = True
        Else
            massubrgtStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("massubrgt", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "status", "sbrgtid", SBRGTIDTxt.Text)
    End Sub

    Private Sub addMassubrgt_Click(sender As Object, e As EventArgs) Handles addMassubrgt.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If STRGTTxt.Text.ToString.Length > 0 And SBRGTTxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New mastypergt
                    objInsertData.sbSTRGTID_ = STRGTTxt.Tag
                    objInsertData.SUBRGTNAME_ = SBRGTTxt.Text
                    objInsertData.STATUS_ = massubrgtStatusChk.Checked
                    objInsertData.insertMassubrgtData()

                    Dim objSelectDataMastypergt As New mastypergt
                    Dim massubrgtData As DataTable
                    massubrgtData = connect.GetTable(objSelectDataMastypergt.selectMassubrgt_)
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    MASSUBRGTGrid.PrimaryGrid.DataSource = massubrgtData

                    SBRGTTxt.Text = ""
                    massubrgtStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMassubrgt_Click(sender As Object, e As EventArgs) Handles editMassubrgt.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If STRGTTxt.Text.ToString.Length > 0 And SBRGTTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลสิทธิ์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New mastypergt
                        objEditData.SBRGTID_ = SBRGTIDTxt.Text
                        objEditData.sbSTRGTID_ = STRGTTxt.Tag
                        objEditData.SUBRGTNAME_ = SBRGTTxt.Text
                        objEditData.STATUS_ = massubrgtStatusChk.Checked
                        objEditData.editMassubrgtData()

                        Dim objSelectDataMastypergt As New mastypergt
                        Dim massubrgtData As DataTable
                        massubrgtData = connect.GetTable(objSelectDataMastypergt.selectMassubrgt_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        MASSUBRGTGrid.PrimaryGrid.DataSource = massubrgtData

                        SBRGTTxt.Text = ""
                        massubrgtStatusChk.Checked = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(Massubrgt.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(Massubrgt.Text)
        nextform.ShowDialog()
    End Sub

   
   
End Class